---
title: "Figures A4 and A5"
output: html_notebook
---

```{r setup}
# Initial Version: Created on August 10, 2018.
# Last Checked: Verified for functionality on February 19, 2025.
# The current version is compatible with R version 4.3.0 and R Studio version 2024.12.0+467.

library(tidyverse)
library(haven)

# please check working directory
getwd()
#you can also mannually set the working directory
#workdir <- "....../ReplicationPackage/Rfiles"
#setwd("workdir")

# center the tile in ggplot
theme_update(plot.title = element_text(hjust = 0.5))
```


# Figures A4 and A5 in the online appendix

Run "10_analysis_reg_supplementary.do" before drawing the following figures 


## QQ for continuous fine
```{r}
temp <- read_stata("../Rdta/finepoly3.dta")
temp %>% 
  filter(str_detect(var, "_eq2")) %>%
  mutate(fine = str_extract(var, "diff_\\d+")) %>%
  mutate(fine = str_remove(fine, "diff_") %>% as.integer()) %>%
  mutate(fine = fine/100) %>%
  ggplot(aes(x = fine, y = coef)) +
  geom_point() +
  geom_line() +
  geom_line(aes(y = ci_lower), linetype = "dashed") +
  geom_line(aes(y = ci_upper), linetype = "dashed") +
  geom_hline(yintercept = 0, linetype = "dotted") + 
  labs(x = "Policy intensity",
       y = "Differences in QQ effects") +
  scale_x_continuous(breaks = seq(0.20,1.20, 0.10)) +
  scale_y_continuous(breaks = seq(-1, 1.5, 0.5)) +
  theme_classic() +  
  theme(strip.background = element_blank(),
        strip.text = element_text(size = 12),
        legend.position = "bottom") 

ggsave("FigureA4_figFinePoly3.pdf", path = "../output",
       width = 8, height = 5)
```


```{r}
temp3 <- read_stata("../Rdta/finepoly3.dta") %>% 
  filter(str_detect(var, "_eq2")) %>%
  mutate(poly = 3)

temp4 <- read_stata("../Rdta/finepoly4.dta") %>% 
  filter(str_detect(var, "_eq2")) %>%
  mutate(poly = 4)

temp5 <- read_stata("../Rdta/finepoly5.dta") %>% 
  filter(str_detect(var, "_eq2")) %>%
  mutate(poly = 5)


poly345 <- bind_rows(temp3, temp4, temp5) %>% 
  mutate(poly = factor(poly, levels = c(3,4,5), 
                       labels = c("3rd", "4th", "5th")
                       )
         ) %>%
  mutate(fine = str_extract(var, "diff_\\d+")) %>%
  mutate(fine = str_remove(fine, "diff_") %>% as.integer()) %>%
  mutate(fine = fine/100) 
  
poly345 %>% 
  ggplot(aes(x = fine, y = coef, linetype = poly)) + 
  geom_line() +
  labs(x = "Policy intensity",
       y = "Differences in QQ Effects") +
  scale_x_continuous(breaks = seq(0.20,1.20, 0.10)) +
  scale_y_continuous(breaks = seq(-1, 1.5, 0.5)) +
  scale_linetype_discrete(name = "Order of polynomials") +
  theme_classic() +  
  theme(strip.background = element_blank(),
        strip.text = element_text(size = 12),
        legend.position = "bottom")   

ggsave("FigureA5_figFinePoly345.pdf", path = "../output",
       width = 8, height = 5)
```


